package com.jrx.anytxn.transaction.timer.job.loan.step;

import com.jrx.anytxn.transaction.bean.AfterSucSaveBean;
import com.jrx.anytxn.transaction.bean.LoanSaveBean;
import com.jrx.anytxn.transaction.entity.*;
import com.jrx.anytxn.transaction.mapper.ext.*;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 放款writer
 */
@Service
public class LoanWriter implements ItemWriter<LoanSaveBean> {

    private static final Logger logger = LoggerFactory.getLogger(LoanWriter.class);


    @Resource
    private ExtAmInterestAccrualInfoMapper extAmInterestAccrualInfoMapper;
    @Resource
    private ExtAmAccountMainInfoMapper extAmAccountMainInfoMapper;
    @Resource
    private ExtTlGlInterfaceInfoMapper extTlGlInterfaceInfoMapper;
    @Resource
    private ExtAmRepaymentPlanInfoMapper extAmRepaymentPlanInfoMapper;
    @Resource
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Resource
    private ExtTlDisbursementLogMapper extTlDisbursementLogMapper;
    @Resource
    private ExtTlTransactionInfoMapper extTlTransactionInfoMapper;
    @Resource
    private ExtAmLoanMainInfoMapper extAmLoanMainInfoMapper;
    @Resource
    private ExtAmWaiveRepaymentPlanInfoMapper extAmWaiveRepaymentPlanInfoMapper;
    @Resource
    private ExtTlInterestAccrualLogMapper extTlInterestAccrualLogMapper;
    @Resource
    private ExtTlLoanClassifyChangeMapper extTlLoanClassifyChangeMapper;

    @Override
    public void write(List items) {


        List<TlDisbursementLog> updateLogList = new ArrayList<>();

        List<TlLoanClassifyChange> tlLoanClassifyChangeList = new ArrayList<>();

        List<AmRepaymentPlanInfo> savePlanList = new ArrayList<>();

        List<AmWaiveRepaymentPlanInfo> saveWaivePlanList = new ArrayList<>();

        List<AmAccountMainInfo> saveAcctList = new ArrayList<>();

        List<AmInterestAccrualInfo> saveAccuAcctList = new ArrayList<>();

        List<TlGlInterfaceInfo> saveGlList = new ArrayList<>();

        List<TlTransactionInfo> tlTransactionInfoList = new ArrayList<>();

        List<AmLoanAccountInfo> updateLoanList = new ArrayList<>();

        List<AmLoanMainInfo> updateLoanMainInfoList = new ArrayList<>();

        List<TlInterestAccrualLog> saveIntrAccrualLogList = new ArrayList<>();

        for (Object item : items) {
            LoanSaveBean loanSaveBean = (LoanSaveBean) item;
            if (null != loanSaveBean.getUpdateLog()) {
                updateLogList.add(loanSaveBean.getUpdateLog());
            }

            if (null != loanSaveBean.getTlLoanClassifyChange()){
                tlLoanClassifyChangeList.add(loanSaveBean.getTlLoanClassifyChange());
            }


            AfterSucSaveBean afterSucSaveBean = loanSaveBean.getAfterSucSaveBean();
            if(null != afterSucSaveBean){
                if(null != afterSucSaveBean.getUpdateLoanMainInfo()){
                    updateLoanMainInfoList.add(afterSucSaveBean.getUpdateLoanMainInfo());
                }

                if(null != afterSucSaveBean.getUpdateLoanAccountInfo()){
                    updateLoanList.add(afterSucSaveBean.getUpdateLoanAccountInfo());
                }

                if (CollectionUtils.isNotEmpty(afterSucSaveBean.getSavePlanList())) {
                    savePlanList.addAll(afterSucSaveBean.getSavePlanList());
                }

                if (CollectionUtils.isNotEmpty(afterSucSaveBean.getSaveWaivePlanList())) {
                    saveWaivePlanList.addAll(afterSucSaveBean.getSaveWaivePlanList());
                }

                if (CollectionUtils.isNotEmpty(afterSucSaveBean.getSaveAcctList())) {
                    saveAcctList.addAll(afterSucSaveBean.getSaveAcctList());
                }

                if (CollectionUtils.isNotEmpty(afterSucSaveBean.getSaveAccuAcctList())) {
                    saveAccuAcctList.addAll(afterSucSaveBean.getSaveAccuAcctList());
                }

                if (CollectionUtils.isNotEmpty(afterSucSaveBean.getSaveGlList())) {
                    saveGlList.addAll(afterSucSaveBean.getSaveGlList());
                }
                if(CollectionUtils.isNotEmpty(afterSucSaveBean.getSaveTransList())){
                    tlTransactionInfoList.addAll(afterSucSaveBean.getSaveTransList());
                }
                if(CollectionUtils.isNotEmpty(afterSucSaveBean.getSaveIntrAccrualLogList())){
                    saveIntrAccrualLogList.addAll(afterSucSaveBean.getSaveIntrAccrualLogList());
                }

            }
        }

        if (CollectionUtils.isNotEmpty(updateLogList)) {
            extTlDisbursementLogMapper.batchUpdateLogForBatch(updateLogList);
        }

        if (CollectionUtils.isNotEmpty(tlLoanClassifyChangeList)){
            extTlLoanClassifyChangeMapper.batchInsert(tlLoanClassifyChangeList);
        }

        if (CollectionUtils.isNotEmpty(updateLoanList)) {
            extAmLoanAccountInfoMapper.batchUpdateLoanAccountForBatch(updateLoanList);
        }

        if (CollectionUtils.isNotEmpty(updateLoanMainInfoList)) {
            extAmLoanMainInfoMapper.batchUpdateLoanMainForBatch(updateLoanMainInfoList);
        }

        if (CollectionUtils.isNotEmpty(savePlanList)) {
            extAmRepaymentPlanInfoMapper.insertAmRepaymentPlanInfoList(savePlanList);
        }

        if (CollectionUtils.isNotEmpty(saveWaivePlanList)) {
            extAmWaiveRepaymentPlanInfoMapper.insertAmWaiveRepaymentPlanInfoList(saveWaivePlanList);
        }


        if (CollectionUtils.isNotEmpty(saveAcctList)) {
            extAmAccountMainInfoMapper.batchInsertAccount(saveAcctList);
        }


        if (CollectionUtils.isNotEmpty(saveAccuAcctList)) {
            extAmInterestAccrualInfoMapper.batchInsertInterestAccrualInfo(saveAccuAcctList);
        }

        if (CollectionUtils.isNotEmpty(saveGlList)) {
            extTlGlInterfaceInfoMapper.batchInsertSelective(saveGlList);
        }

        if(CollectionUtils.isNotEmpty(tlTransactionInfoList)){
            extTlTransactionInfoMapper.batchInsert(tlTransactionInfoList);
        }

        if(CollectionUtils.isNotEmpty(saveIntrAccrualLogList)){
            extTlInterestAccrualLogMapper.batchInsert(saveIntrAccrualLogList);
        }

    }
}
